package com.jwthhealth_pub.old.signutil.model.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.db.DbHelper;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by liuzhen000 on 2016/12/1.
 */

public class UserInfoDao {
    private static UserInfoDao instance;
    private Context context;
    private Dao<LoginModel.DataBean, Integer> userInfoDao;
    private DbHelper databaseHelper;
    private LoginModel.DataBean dataBean;

    @SuppressWarnings("unchecked")
    public UserInfoDao(Context context) {
        super();
        this.context = context;

        try {
            databaseHelper = DbHelper.getHelper(context);
            userInfoDao = databaseHelper.getDao(LoginModel.DataBean.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static UserInfoDao getInstance() {

        if (instance == null) {
            instance = new UserInfoDao(App.mContext);
        }

        return instance;
    }

    /**
     * 插入一条数据
     * @param userInfoModel
     * @return
     */
    public boolean add(LoginModel.DataBean userInfoModel){
        boolean isInsert = false;

        try {
            int result = userInfoDao.create(userInfoModel);
            if (result > 0)
                isInsert = true;
        } catch (SQLException e) {
            Log.e("UserInfoDao", e.toString());
        }

        return isInsert;
    }

    /*
    * 查询Userinfo
    */
    public LoginModel.DataBean queryUserinfoModel() {


        try {
            List<LoginModel.DataBean> dataBeens = userInfoDao.queryForAll();
            Log.d("UserInfoDao", "dataBeens.size():" + dataBeens.size());
            if (dataBeens.size() > 0) {
                dataBean = dataBeens.get(0);
            } else {
                dataBean = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dataBean;
    }

    /**
     * 删除用户信息
     * @return
     */
    public boolean clearUserInfo(String uid){
        boolean isClear = false;

        try {
            DeleteBuilder<LoginModel.DataBean, Integer> dataBeanIntegerDeleteBuilder = userInfoDao.deleteBuilder();
            dataBeanIntegerDeleteBuilder.where().eq("id", uid);
            int delete = dataBeanIntegerDeleteBuilder.delete();

            Log.d("UserInfoDao", "delete:" + delete);
            if (delete > 0){
                isClear = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isClear;
    }

    /**
     * 更新用户名
     * @param un
     * @return
     */
    public boolean updateUserName(String un) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("username", un);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }

    /**
     * 更新用户电话信息
     * @param phone
     * @return
     */
    public boolean updateUserPhone(String phone) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("phone", phone);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }

    /**
     * 修改邮箱
     * @param email
     * @return
     */
    public boolean updateUserEmail(String email) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("email", email);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }


    /**
     * 修改性别
     * @param choiceSex
     * @return
     */
    public boolean updateUsersex(int choiceSex) {
        boolean isUpdate = true;
        String sex = "";
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            if (choiceSex == 0){
                sex = "男";
                updateBuilder.updateColumnValue("sex", sex);
            } else if (choiceSex == 1) {
                sex = "女";
                updateBuilder.updateColumnValue("sex", sex);
            }
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }

    /**
     * 更新日期
     * @param data
     * @return
     */
    public boolean updateUserData(String data) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("birthday", data);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }

    /**
     * 修改身高
     * @param height
     * @return
     */
    public boolean updateUserHeight(String height) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("height", height);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }

    /**
     * 修改体重
     * @param weight
     * @return
     */
    public boolean updateUserWeight(String weight) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("weight", weight);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }


    /**
     * 更新地址
     * @param address
     * @return
     */
    public boolean updateUserAddress(String address) {
        boolean isUpdate = true;
        try {
            UpdateBuilder<LoginModel.DataBean, Integer> updateBuilder = userInfoDao.updateBuilder();
            updateBuilder.updateColumnValue("address", address);
            int update = updateBuilder.update();
            if (update > 0) {
                isUpdate = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isUpdate;
    }
}
